package org.jeecg.modules.wangyue.controller.upload;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.minio.messages.Upload;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.wangyue.constant.CommonConstant;
import org.jeecg.modules.wangyue.entity.Advertisement;
import org.jeecg.modules.wangyue.service.IAdvertisementService;
import org.jeecg.modules.wangyue.utils.FileUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.util.Arrays;

/**
 * APP上传功能
 */
@Slf4j
@Api(tags="APP上传功能")
@RestController
@RequestMapping("/wangyue/app/appUpload")
public class AppUploadController {


    /**
     * 上传文件
     * @param file          上传文件
     * @param userId        用户id
     * @param uploadType    上传类型
     * @param oldFileUrl    旧文件的url
     * @return
     */
    @AutoLog(value = "上传文件")
    @ApiOperation(value="上传文件", notes="上传文件")
    @PostMapping(value = "/uploadFile")
    public Result<?> uploadFile(MultipartFile file,
                                @RequestParam(name="uploadType",required = true) String uploadType,
                                @RequestParam(name="userId",required = false) String userId,
                                @RequestParam(name="orderNo",required = false) String orderNo,
                                @RequestParam(name="oldFileUrl",required = false) String oldFileUrl){
        if(oConvertUtils.isEmpty(uploadType)){
            return Result.error("上传类型uploadType不能为空");
        }else if(oConvertUtils.isEmpty(file)){
            return Result.error("上传文件file不能为空");
        }else if(oConvertUtils.isEmpty(userId) && oConvertUtils.isEmpty(orderNo)){
            return Result.error("用户id、订单号不能同时传空值");
        }

        String addPath = File.separator + UploadPath.common + File.separator + UploadPath.app + File.separator + userId; // 值为: /upload/app/userId的值
        if(UploadPath.userHead.equals(uploadType)){ //消费者头像
            addPath = addPath + File.separator + UploadPath.userHeadPath;

        }else if(UploadPath.jsRealPic.equals(uploadType)){ //技师真实正面照
            addPath = addPath + File.separator + UploadPath.jsRealPicPath;

        }else if(UploadPath.jsIdCardFront.equals(uploadType)){ //技师身份证(正面)
            addPath = addPath + File.separator + UploadPath.jsIdCardFrPath;

        }else if(UploadPath.jsIdCardBack.equals(uploadType)){ //技师身份证(背面)
            addPath = addPath + File.separator + UploadPath.jsIdCardBaPath;

        }else if(UploadPath.jsVideoAuth.equals(uploadType)){ //技师视频认证
            addPath = addPath + File.separator + UploadPath.jsVideoAuPath;

        }else if(UploadPath.jsSignContract.equals(uploadType)){ //技师签署合同
            addPath = addPath + File.separator + UploadPath.jsSignContractPath;

        }else if(UploadPath.problemFeedback.equals(uploadType)){ //问题反馈(图片、视频)
            addPath = addPath + File.separator + UploadPath.problemFeedbackPath;

        }else if(UploadPath.jsTrends.equals(uploadType)){ //技师动态发布
            addPath = addPath + File.separator + UploadPath.jsTrendsPath;

        }else if(UploadPath.serviceBegin.equals(uploadType)){ //订单-服务开始
            addPath = File.separator + UploadPath.common + File.separator + UploadPath.order; // 值为: /upload/order的值
            addPath = addPath + File.separator + orderNo + File.separator + UploadPath.serviceBeginPath;

        }else if(UploadPath.serviceFinish.equals(uploadType)){ //订单-服务完成
            addPath = File.separator + UploadPath.common + File.separator + UploadPath.order; // 值为: /upload/order的值
            addPath = addPath + File.separator + orderNo + File.separator + UploadPath.serviceFinishPath;

        }
        String url = FileUtil.uploadNewAndDelOld(file, oldFileUrl, addPath);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("url", url);
        return Result.OK(jsonObject);
    }



    /**
     * 删除文件
     * @param fileUrl 文件路径
     * @return
     */
    @AutoLog(value = "删除文件")
    @ApiOperation(value="删除文件", notes="删除文件")
    @PostMapping(value = "/delFile")
    public Result<?> delFile(@RequestParam(name="fileUrl",required = true) String fileUrl){
        if(oConvertUtils.isEmpty(fileUrl)){
            return Result.error("上传文件不能为空");
        }

        //默认头像不需要删除，直接响应即可
        if(CommonConstant.defaultHeadUrl.equals(fileUrl)){
            return Result.OK("文件删除成功");
        }

        //删除文件
        if(FileUtil.delFile(fileUrl)){
            return Result.OK("文件删除成功");
        }else {
            return Result.error("文件删除失败");
        }
    }


}
